<html>
<head><meta charset="utf-8"><title>Understanding fmt internals · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html">Understanding fmt internals</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="231312888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231312888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231312888">(Mar 22 2021 at 13:51)</a>:</h4>
<p>Why do we have <a href="https://github.com/rust-lang/rust/blob/61edfd591cedff66fca639c02f66984f6271e5a6/library/core/src/fmt/rt/v1.rs#L42"><code>Count::Param</code></a> and <code>ArgumentV1::from_usize</code>? Couldn't we just copy the value and just use <code>Count::Is</code> instead? (and then it could just be <code>Option&lt;usize&gt;</code>?) I've been doing some experimenting to see if I can simplify/speed up <code>format_args!</code>.</p>



<a name="231327964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231327964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231327964">(Mar 22 2021 at 15:19)</a>:</h4>
<p><span class="user-mention" data-user-id="360405">@Cameron Steffen</span> because the Counts (and all other formatting metadata) are stored in read only memory, they're static.</p>



<a name="231328444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231328444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231328444">(Mar 22 2021 at 15:21)</a>:</h4>
<p><span class="user-mention" data-user-id="360405">@Cameron Steffen</span> I already made an experimental implementation making it all non-static, so we can get rid of Count::Param: <a href="https://github.com/rust-lang/rust/pull/78416/">https://github.com/rust-lang/rust/pull/78416/</a></p>



<a name="231329729"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231329729" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231329729">(Mar 22 2021 at 15:28)</a>:</h4>
<p>I see. Is that something you are still pursuing?</p>



<a name="231332460"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231332460" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231332460">(Mar 22 2021 at 15:42)</a>:</h4>
<p>I came up with a slightly different approach where I basically merge the list of <code>str</code> with the list of args, so each arg has a <code>str</code> prefix.</p>



<a name="231333064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231333064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231333064">(Mar 22 2021 at 15:45)</a>:</h4>
<p><span class="user-mention" data-user-id="360405">@Cameron Steffen</span> the only way it'll be fast is if you separate all the static data from the dynamic data. if any element of an array contains something dynamic (e.g. an argument), it will have to be constructed entirely on the stack.</p>



<a name="231333297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231333297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231333297">(Mar 22 2021 at 15:46)</a>:</h4>
<p>I have a new implementation (not public yet) that puts as much information as possible in read only memory. fmt::Arguments is then just two pointers. one to the arguments on the stack (without vtable pointers!), and one to all other data in one block in read only memory.</p>



<a name="231333331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231333331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231333331">(Mar 22 2021 at 15:46)</a>:</h4>
<p>but it's not complete yet</p>



<a name="231333765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231333765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231333765">(Mar 22 2021 at 15:49)</a>:</h4>
<p>Interesting. Is the read-only portion basically everything derived from the format string?</p>



<a name="231333808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231333808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231333808">(Mar 22 2021 at 15:49)</a>:</h4>
<p>(not the args)</p>



<a name="231334089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231334089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231334089">(Mar 22 2021 at 15:50)</a>:</h4>
<p><span class="user-mention" data-user-id="310399">@Mara</span> What is the difference between the v2 fmt and the current one?</p>



<a name="231334197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231334197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231334197">(Mar 22 2021 at 15:51)</a>:</h4>
<p>which  v2?</p>



<a name="231334232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231334232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231334232">(Mar 22 2021 at 15:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="360405">Cameron Steffen</span> <a href="#narrow/stream/219381-t-libs/topic/Understanding.20fmt.20internals/near/231333765">said</a>:</p>
<blockquote>
<p>Interesting. Is the read-only portion basically everything derived from the format string?</p>
</blockquote>
<p>yes.</p>



<a name="231334395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231334395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231334395">(Mar 22 2021 at 15:52)</a>:</h4>
<p>everything that doesn't change for different invocations at runtime.</p>



<a name="231334724"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231334724" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231334724">(Mar 22 2021 at 15:54)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/78416/">https://github.com/rust-lang/rust/pull/78416/</a></p>



<a name="231335110"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231335110" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231335110">(Mar 22 2021 at 15:56)</a>:</h4>
<p>that's not v2. that's just some experiment that shows it's important to keep the read-only data separate.</p>



<a name="231335624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231335624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231335624">(Mar 22 2021 at 15:59)</a>:</h4>
<p>Makes sense. Then maybe any equal format strings could be collapsed to the same <code>const ParsedFormat</code>.</p>



<a name="231335859"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231335859" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231335859">(Mar 22 2021 at 16:00)</a>:</h4>
<p>Anyways I'll leave you to it. Thanks for filling me in!</p>



<a name="231336912"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231336912" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231336912">(Mar 22 2021 at 16:05)</a>:</h4>
<p><span class="user-mention" data-user-id="360405">@Cameron Steffen</span> it's not just the format string. there's also the function pointers to the display functions for each argument.</p>



<a name="231336978"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231336978" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231336978">(Mar 22 2021 at 16:05)</a>:</h4>
<p>right now those are stored next to the arguments, on the stack. but that's a bit of a waste, because they're constant</p>



<a name="231337115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Understanding%20fmt%20internals/near/231337115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Understanding.20fmt.20internals.html#231337115">(Mar 22 2021 at 16:06)</a>:</h4>
<p>but separating those turns out to be very tricky. it's quite hard to get those function pointers in a <code>const</code> context in rust</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>